import * as type from './mutation-types'
import * as API from './api'
import { ajax } from '@/utils/fetch'
const getParams = (data) => {
    let params = { ...data }
    if (params.cb) { params.cb = '' }
    return params
}
export const setCheckedDayList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { checkDayList: data.list })
};
export const getDayList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { dayListData: {} })

    // http 请求
    return ajax.get(
        API.getDayList,
        { commit },
        getParams(data),
        type.SET_DAY_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getOSList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { osListData: {} })

    // http 请求
    return ajax.get(
        API.getOSList, {
        commit
    },
        getParams(data),
        type.SET_OS_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getMediaList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { mediaListData: {} })
    // http 请求
    return ajax.get(
        API.getMediaList, {
        commit
    },
    getParams(data),
        type.SET_MEDIA_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getMediaSearchList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { mediaSearchList: [] })
    // http 请求
    return ajax.get(
        API.getMediaSearchList, {
        commit
    },
        data,
        type.SET_MEDIA_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};

export const getGeoList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { geoListData: {} })
    // http 请求
    return ajax.get(
        API.getGeoList, {
        commit
    },
    getParams(data),
        type.SET_GEO_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getGeoSearchList = ({ commit }, data) => {
    // http 请求前分发action
    commit(type.RESET_STATE_ITEM, { geoSearchList: [] })
    // http 请求
    return ajax.get(
        API.getGeoSearchList, {
        commit
    },
        data,
        type.SET_GEO_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};


export const getSeriesList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { seriesListData: {} })
    return ajax.get(
        API.getSeriesList, {
        commit
    },
    getParams(data),
        type.SET_SERIES_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getGroupList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { groupListData: {} })
    return ajax.get(
        API.getGroupList, {
        commit
    },
    getParams(data),
        type.SET_GROUP_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getMainList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { mainListData: {} })
    return ajax.get(
        API.getMainList, {
        commit
    },
    getParams(data),
        type.SET_MAIN_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getSeriesByGeoList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { seriesByGeoListData: {} })
    return ajax.get(
        API.getSeriesByGeoList, {
        commit
    },
    getParams(data),
        type.SET_SERIES_BY_GEO_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getGroupByGeoList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { groupByGeoListData: {} })
    return ajax.get(
        API.getGroupByGeoList, {
        commit
    },
    getParams(data),
        type.SET_GROUP_BY_GEO_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getMainByGeoList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { mainByGeoListData: {} })
    return ajax.get(
        API.getMainByGeoList, {
        commit
    },
    getParams(data),
        type.SET_MAIN_BY_GEO_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};

export const getSeriesSearchList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { seriesSearchList: [] })
    return ajax.get(
        API.getSeriesSearchList, {
        commit
    },
        data,
        type.SET_SERIES_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getGroupSearchList = ({ commit }, data) => {    
    commit(type.RESET_STATE_ITEM, { groupSearchList: [] })
    return ajax.get(
        API.getGroupSearchList, {
        commit
    },
        data,
        type.SET_GROUP_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getMainSearchList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { mainSearchList: [] })
    return ajax.get(
        API.getMainSearchList, {
        commit
    },
        data,
        type.SET_MAIN_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
}; 

// 调用下载接口
export const downloadFile = ({commit}, data) => {    
    let url , res
    let obj = {
        'day': API.downloadAdDay,
        'os': API.downloadAdOs,
        'media': API.downloadAdMedia,
        'geo': API.downloadAdGeo,
        'series': data.isGeo ? API.downloadAdSeriesGeo : API.downloadAdSeries,
        'group': data.isGeo ? API.downloadAdGroupGeo : API.downloadAdGroup,
        'main': data.isGeo ? API.downloadAdMainGeo : API.downloadAdMain,
        'channelMedia': API.downloadAdchannel
    }  
    url = obj[data.type]   
    commit(type.RESET_STATE_ITEM, {downloadData: {}})
    res = type.DOWNLOAD_DATA_RECEIVE
    return ajax.get(
        url, {
            commit
        },
        data,
        res
    )
}

// 获取表格合计数据  GET_TOTAL_DATA
export const getTotalData = ({ commit}, data) => {    
    let url
    let obj = {
        'day': API.getDayWeighting,
        'os': API.getOsWeighting,
        'media': API.getMediaWeighting,
        'geo': API.getGeoWeighting,
        'series': data.isGeo ? API.getSeriesByGeoWeighting : API.getSeriesWeighting,
        'group': data.isGeo ? API.getGroupByGeoWeighting : API.getGroupWeighting,
        'main': data.isGeo ? API.adByGeoWeighting : API.adWeighting,
        'channelMedia': API.channelWeighting
    }  
    url = obj[data.type]
    commit(type.RESET_STATE_ITEM, {getTotalData: {}})
    // res = type.DOWNLOAD_DATA_RECEIVE
    return ajax.get(
        url, {
            commit
        },
        data,
        type.GET_TOTAL_DATA
    )
}

export const getChannelList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { channelListData: {} })
    return ajax.get(
        API.getChannelList, {
        commit
    },
    getParams(data),
        type.SET_CHANNEL_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getChannelSearchList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { channelSearchList: [] })
    return ajax.get(
        API.getChannelSearchList, {
        commit
    },
    getParams(data),
        type.SET_CHANNEL_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getChannelCampaignSearchList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { channelCampaignSearchList: [] })
    return ajax.get(
        API.getChannelCampaignSearchList, {
        commit
    },
    getParams(data),
        type.SET_CHANNEL_CAMPAIGN_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
export const getChannelAdgroupSearchList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { channelAdgroupSearchList: [] })
    return ajax.get(
        API.getChannelAdgroupSearchList, {
        commit
    },
    getParams(data),
        type.SET_CHANNEL_ADGROUP_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};

export const getChannelAdSearchList = ({ commit }, data) => {
    commit(type.RESET_STATE_ITEM, { channelAdSearchList: [] })
    return ajax.get(
        API.getChannelAdSearchList, {
        commit
    },
    getParams(data),
        type.SET_CHANNEL_AD_SEARCH_LIST
    ).finally(() => {
        data.cb && data.cb()
    })
};
